﻿using LiteNetLib;
using LiteNetLib.Utils;
using System.Collections.Generic;
using System.Net;

public class SimpleNet
{
    public NetManager NetManager;
    public NetPacketProcessor NetPacketProcessor;
    public bool Start(int port, EventBasedNetListener listener, NetPacketProcessor processor,
        bool broadcastReceiveEnabled = false, bool unconnectedMessagesEnabled = false, int updateTime = 10)
    {
        NetPacketProcessor = processor;
        NetManager = new NetManager(listener)
        {
            BroadcastReceiveEnabled = broadcastReceiveEnabled,
            UnconnectedMessagesEnabled = unconnectedMessagesEnabled,
            UpdateTime = updateTime
        };
        return NetManager.Start(port);
    }

    public void Broadcast<T>(ref T packet, DeliveryMethod options, NetPeer ?except = null) where T : INetSerializable
    {
        foreach (var kv in NetManager.PeersDict)
        {
            if (kv.Value == except)
            {
                continue;
            }

            NetPacketProcessor.SendNetSerializable(kv.Value, ref packet, options);
        }
    }

    public void Update()
    {
        NetManager?.PollEvents();
    }

    public void Destroy()
    {
        NetManager?.Stop();
    }
}
